% Calculate polynomial coefficient functions
%Requires the file [Hermite_Polynomials.m]
%% ------------------------- [Description] ---------------------------
%coeMat------ coefficient matrix.                                      
%inVarMat---- input variable matrix with the number of rows as the number of matching points and the number of columns as the number of dimensions.          
%outVarMat --- output variable matrix with the same number of rows as above and the same number of columns as coeMat.        
%polOrd------ polynomial order. 
%% ------------------------------------------------------------
function [coeMat,polChaos]=polCoeCal(inVarMat,outVarMat,polOrd)
dimNum=length(inVarMat(1,:)); %dimensions
colNum=length(inVarMat(:,1)); % number of matching points
termNum=nchoosek((polOrd+dimNum),dimNum); % number of terms                   
% Calculate the matching point matrix
colValMat=ones(colNum,termNum); % initialize the matrix of matching points carried into polynomial values
for j=1:colNum
    polTerm=Hermite_Polynomials(polOrd,dimNum,'x');
    for i=1:dimNum
        polTerm=subs(polTerm,['x',num2str(i)],inVarMat(j,i));%substitute the matching point values into the polynomial
    end
    colValMat(j,:)=polTerm';
end
% Calculate the coefficient matrix and polynomial
coeMat=pinv(colValMat)*outVarMat; %Find the coefficient matrix, using the pseudo-inverse
polTerm=Hermite_Polynomials(polOrd,dimNum,'x'); %Get the polynomial again
polChaos=vpa(simplify(sum(coeMat'*polTerm)),8); %Get polynomial
end